﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
using System.Reflection;
using V5_Common;

namespace V5_Data {
    public class V5UrlHelper {
        private static readonly V5UrlHelper m_Instance = new V5UrlHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5UrlHelper Instance {
            get { return m_Instance; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Controller"></param>
        /// <returns></returns>
        public string SplitController(string Controller) {
            if (Controller.IndexOf("-") > -1) {
                if (Controller.IndexOf("-") > -1) {
                    Controller = Controller.Replace("-", "/");
                    //Controller += "/";
                }
            }
            return Controller;
        }

        public string Action(string Controller, object attributes, int ShowType) {
            //if (Controller.IndexOf("-") > -1) {
            //    Controller = Controller.Replace("-", "/");
            //    Controller += "/";
            //}
            StringBuilder sResult = new StringBuilder();
            sResult = new StringBuilder();
            if (ShowType == 1) {
                sResult.Append("index.aspx?t=" + Controller);
                PropertyInfo[] pp = attributes.GetType().GetProperties();
                foreach (PropertyInfo p in pp) {
                    string value = p.GetValue(attributes, null).ToString();
                    sResult.Append("&" + p.Name + "=" + value);
                }
            }
            else if (ShowType == 2) {
                sResult.Append(Controller);
                PropertyInfo[] pp = attributes.GetType().GetProperties();
                foreach (PropertyInfo p in pp) {
                    string value = p.GetValue(attributes, null).ToString();
                    sResult.Append("-" + value);
                }
                sResult.Append(".aspx");
            }
            else if (ShowType == 3) {
                Controller = Controller.Replace("-", "/");
                Controller = "/" + Controller + "/";
                sResult.Append(Controller);
                PropertyInfo[] pp = attributes.GetType().GetProperties();
                foreach (PropertyInfo p in pp) {
                    string value = p.GetValue(attributes, null).ToString();
                    sResult.Append(value + "-");
                }
                if (sResult.Length > 0) {
                    sResult = sResult.Remove(sResult.Length - 1, 1);
                }
                sResult.Append(".html");
            }
            return "/" + sResult.ToString();
        }

        public string Action(string Controller, IDictionary attributes, int ShowType) {
            StringBuilder sResult = new StringBuilder();
            sResult = new StringBuilder();
            if (ShowType == 1) {
                sResult.Append("index.aspx?t=" + Controller);
                if (attributes != null) {
                    foreach (object key in attributes.Keys) {
                        object value = attributes[key];
                        sResult.Append("&" + key + "=" + value);
                    }
                }
            }
            else if (ShowType == 2) {
                sResult.Append(Controller);
                if (attributes != null) {
                    foreach (object key in attributes.Keys) {
                        object value = attributes[key];
                        sResult.Append("-" + value);
                    }
                }
                sResult.Append(".aspx");
            }
            else if (ShowType == 3) {
                Controller = Controller.Replace("-", "/");
                Controller = "/" + Controller + "/";
                sResult.Append(Controller);
                if (attributes != null) {
                    foreach (object key in attributes.Keys) {
                        object value = attributes[key];
                        sResult.Append(value + "-");
                    }
                }
                if (sResult.Length > 0) {
                    sResult = sResult.Remove(sResult.Length - 1, 1);
                }
                sResult.Append(".html");
            }
            return "/" + sResult.ToString();
        }

        public string Action(string Controller, IDictionary attributes) {
            int ShowType = 0;// InputHelper.SetNumber(V5.Config.View["SiteShow"]);
            ShowType = 1;
            return Action(Controller, attributes, ShowType);
        }

        //public string Action(string Controller, object attributes) {
        //    int ShowType = 1;//InputHelper.SetNumber(V5.Config.View["SiteShow"]);
        //    return Action(Controller, attributes, ShowType);
        //}

        public string Action(string Controller, object attributes) {
            int ShowType = 1;//InputHelper.SetNumber(V5.Config.View["SiteShow"]);
            return Action(Controller, attributes, ShowType);
        }
    }
}